﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ring2Park.Models;

namespace Ring2Park.Controllers
{ 
    public class VehicleController : Controller
    {
        private Ring2ParkEntities db = new Ring2ParkEntities();

        //
        // GET: /Vehicle/

        public ActionResult Index()
        {
            var vehicles = from v in db.Vehicles
                           where v.UserName == User.Identity.Name
                           select v;

            if (vehicles.Count() > 0)
                return View(vehicles.ToList());
            else
                return View("NoVehiclesFound");
        }

        //
        // GET: /Vehicle/Details/5

        public ActionResult Details(int id = 0)
        {
            Vehicle vehicle = db.Vehicles.Find(id);
            if (vehicle == null)
                return HttpNotFound();
            else
                return View(vehicle);
        }

        //
        // GET: /Vehicle/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Vehicle/Create

        [HttpPost]
        public ActionResult Create(Vehicle vehicle)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    vehicle.UserName = User.Identity.Name;
                    db.Vehicles.Add(vehicle);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch (DataException)    
            {        
                //Log the error (add a variable name after DataException)        
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");    
            } 
            return View(vehicle);
        }
        
        //
        // GET: /Vehicle/Edit/5
 
        public ActionResult Edit(int id = 0)
        {
            Vehicle vehicle = db.Vehicles.Find(id);
            if (vehicle == null)
                return HttpNotFound();
            else
                return View(vehicle);
        }

        //
        // POST: /Vehicle/Edit/5

        [HttpPost]
        public ActionResult Edit(Vehicle vehicle)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Entry(vehicle).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch (DataException)
            {
                //Log the error (add a variable name after DataException)        
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            } 
            return View(vehicle);
        }

        //
        // GET: /Vehicle/Delete/5

        public ActionResult Delete(int id, bool? saveChangesError)
        {
            if (saveChangesError.GetValueOrDefault())
            {
                ViewBag.ErrorMessage = "Unable to save changes. Try again, and if the problem persists see your system administrator.";
            }

            Vehicle vehicle = db.Vehicles.Find(id);
            if (vehicle == null)
                return HttpNotFound();
            else
                return View(vehicle);
        }

        //
        // POST: /Vehicle/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                Vehicle vehicle = db.Vehicles.Find(id);
                db.Vehicles.Remove(vehicle);
                db.SaveChanges();
            }
            catch (DataException)    
            {        
                //Log the error (add a variable name after DataException)        
                return RedirectToAction("Delete",            
                    new System.Web.Routing.RouteValueDictionary {                 
                        { "id", id },                 
                        { "saveChangesError", true } 
                });    
            }   

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}